package star.cloud.asme.system.dictionary;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import star.cloud.asme.base.dto.Result;
import star.cloud.asme.base.mybatis.IBaseServiceImpl;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class DictionaryServiceImpl extends IBaseServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    private final DictionaryMapper dictionaryMapper;

    @Override
    public Result<IPage<Dictionary>> getListByConditionsWithPage(Dictionary conditions) {
        IPage<Dictionary> iPage = super.getListByConditionsWithPage(conditions).getBody();
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dictionary::getTypeText, conditions.getTypeText());
        return Result.ok(iPage);
    }

    @Override
    public Result<List<Dictionary>> getListByConditions(Dictionary conditions) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Dictionary::getSequence);
        List<Dictionary> dictionaryList = conditions.getDeleted() ? dictionaryMapper.selectListDeleted() : dictionaryMapper.selectList(queryWrapper);
        List<Dictionary> dictionaries = dictionaryList.stream().filter(dictionary -> dictionary.getTypeText().equals(conditions.getTypeText())).toList();
        return Result.ok(dictionaries);
    }

    @Override
    public Result<Dictionary> insertOne(Dictionary entity) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dictionary::getTenant, entity.getTenant());
        queryWrapper.eq(Dictionary::getTypeText, entity.getTypeText());
        queryWrapper.eq(Dictionary::getLabel, entity.getLabel());
        Dictionary dictionarySelected = dictionaryMapper.selectOne(queryWrapper);
        if (Objects.nonNull(dictionarySelected)) {
            return Result.error(dictionarySelected).msg(entity.getLabel() + "已存在，请修改。");
        }
        int inserted = dictionaryMapper.insert(entity);
        Dictionary dictionary = dictionaryMapper.selectById(entity.getId());
        setCreatorAndUpdater(dictionary);
        return inserted > 0 ? Result.ok(dictionary) : Result.error();
    }


    @Override
    public Result<Dictionary> updateOne(Dictionary entity) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dictionary::getTenant, entity.getTenant());
        queryWrapper.eq(Dictionary::getTypeText, entity.getTypeText());
        queryWrapper.eq(Dictionary::getLabel, entity.getLabel());
        queryWrapper.ne(Dictionary::getId, entity.getId());
        Dictionary dictionaryFound = dictionaryMapper.selectOne(queryWrapper);
        if (dictionaryFound != null) {
            return Result.error(dictionaryFound).msg(entity.getLabel() + "已存在。");
        }
        setVersion(entity);
        dictionaryMapper.updateById(entity);
        Dictionary dictionaryUpdated = dictionaryMapper.selectById(entity.getId());
        setCreatorAndUpdater(dictionaryUpdated);
        return Result.ok(dictionaryUpdated).msg("更新字典成功");
    }

    @Override
    public Result<Dictionary> changeSequence(Long id, String direction) {
        Dictionary dictionary = dictionaryMapper.selectById(id);
        if (Objects.equals(direction, "minus")) {
            LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dictionary::getTenant, dictionary.getTenant());
            queryWrapper.eq(Dictionary::getTypeText, dictionary.getTypeText());
            queryWrapper.eq(Dictionary::getSequence, dictionary.getSequence() - 1);
            Dictionary dictionaryBefore = dictionaryMapper.selectOne(queryWrapper);
            dictionaryBefore.setSequence(dictionaryBefore.getSequence() + 1);
            dictionary.setSequence((dictionary.getSequence() - 1));
            dictionaryMapper.updateById(dictionaryBefore);
        } else {
            LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dictionary::getTenant, dictionary.getTenant());
            queryWrapper.eq(Dictionary::getTypeText, dictionary.getTypeText());
            queryWrapper.eq(Dictionary::getSequence, dictionary.getSequence() + 1);
            Dictionary dictionaryAfter = dictionaryMapper.selectOne(queryWrapper);
            dictionary.setSequence((dictionary.getSequence() + 1));
            dictionaryAfter.setSequence(dictionaryAfter.getSequence() - 1);
            dictionaryMapper.updateById(dictionaryAfter);
        }
        dictionaryMapper.updateById(dictionary);
        return Result.ok(dictionaryMapper.selectById(dictionary.getId()));
    }

    @Override
    public Result<Integer> getMaxSequence(String typeText) {
        Integer maxSequence = dictionaryMapper.selectMaxSequence(typeText);
        return maxSequence == null ? Result.ok(0) : Result.ok(maxSequence);
    }
}